/**
 * @format
 * @Author: TC
 * @Date: 2022-03-04 12:35:50
 * @Last Modified by: TC
 * @Last Modified time: 2025-06-20 13:19:14
 */

import { BoxCollider2D, Node, Prefab, UITransform, Vec3, _decorator, v3 } from "cc";
import { EntityComponent } from "db://assets/FrameWork/Module/Ecs/EntityComponent";
import { app } from "../../../FrameWork/App";
import { EntityAction } from "./Propterty/EntityAction";
import { EntityCmd } from "./Propterty/EntityCmd";
import { EntityKeyboard } from "./Propterty/EntityKeyboard";
import { EntitySpeed } from "./Propterty/EntitySpeed";
import { EntitySkill } from "./Propterty/EntitySkill";
import { myGame } from "../../MyGame";
const { ccclass, property } = _decorator;

@ccclass("EntityBase")
export class EntityBase extends EntityComponent {
    @property({ type: Node, displayName: "发射点" })
    nodeCast: Node = null;
    @property({ type: Prefab, displayName: "测试子弹预制" })
    preBullet: Prefab = null;

    private _onGround: boolean = false;
    public set onGround(g: boolean) {
        this._onGround = g;
    }
    public get onGround(): boolean {
        return this._onGround;
    }

    //物理速度
    private _speed: EntitySpeed = null;
    public get speed(): EntitySpeed {
        return this._speed;
    }
    //输入命令
    private _cmd: EntityCmd = null;
    public get cmd(): EntityCmd {
        return this._cmd;
    }
    //动作
    private _act: EntityAction = null;
    public get act(): EntityAction {
        return this._act;
    }
    //技能
    private _skill: EntitySkill = null;
    public get skill(): EntitySkill {
        return this._skill;
    }
    //键盘输入
    private _keyboard: EntityKeyboard = null;
    public get keyboard(): EntityKeyboard {
        return this._keyboard;
    }

    protected _castWorldPos: Vec3 = v3();
    protected _castLocalPos: Vec3 = v3();
    protected init(): void {
        this._skill = new EntitySkill(this);
        this._keyboard = new EntityKeyboard(this);
        this._speed = new EntitySpeed(this);
        this._cmd = new EntityCmd(this);
        this._act = new EntityAction(this);
        super.init();
    }

    public tick(dt: number): void {
        this._cmd.tick();
        this._speed.tick(dt);
        this._act.tick();
        this._skill.tick(dt);
    }

    public onFire(): void {}
    public release(): void {
        app.entityMgr.remove(this.uid);
    }

    /**
     * 获取发射点在子弹层级的坐标
     * @returns
     */
    public getCastPos(): Vec3 {
        this.nodeCast.getWorldPosition(this._castWorldPos);
        myGame.battleMgr.utfBulletLayer.convertToNodeSpaceAR(this._castWorldPos, this._castLocalPos);
        return this._castLocalPos;
    }
}
